package xin.ssm.jxc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;


import xin.ssm.jxc.model.Users;
import xin.ssm.jxc.services.IUsersService;
import xin.ssm.jxc.util.Result;

@Controller
@RequestMapping("/user")
public class UsersController {
	@Resource
	private IUsersService usersService;
	@RequestMapping("/preuser")
	@ResponseBody
	public List<Users> findEmployee() {
		List<Users> list = usersService.findCommonUser("1");
		return list;
	}
	// 查重用户名
	@RequestMapping(value = "/checkuser", method = RequestMethod.POST)
	@ResponseBody
	public String checkUsers(@RequestParam(value = "userLoginName") String username) {
		Users user = usersService.findUserByPrimaryKey(username);
		if (user == null) {
			return "1";
		} else {
			return "0";
		}
	}

	// 添加普通用户
	@RequestMapping("/adduser")
	@ResponseBody
	public String addUsers(@RequestParam(value = "account") String username,
			@RequestParam(value = "password") String password) {
		Users u = new Users();
		u.setUserloginname(username);
		u.setUserloginpassword(password);
		u.setUsertype("1");
		int ret = usersService.addUserSelective(u);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}

	// 普通用户登录
	@RequestMapping("/userlogin")
	@ResponseBody
	public Result UsersLogin(@RequestParam(value = "account") String username,
			@RequestParam(value = "password") String password) {
		Users u = new Users();
		Result result = new Result();
		u.setUserloginname(username);
		u.setUserloginpassword(password);
		Users user = usersService.findUserByNameAndPwd(u);
		if (user != null) {
			List<Users> users = new ArrayList<Users>();
			users.add(user);
			result.setCode(0);
			result.setMsg("1");
			result.setCount(1);
			result.setData(users);
		} else {
			result.setMsg("0");
		}
		return result;
	}

	// 查询所有普通用户登录
	@RequestMapping("/getCommonUser")
	@ResponseBody
	public Result UsersCommon() {
		List<Users> userList = usersService.findCommonUser("1");
		// 时间转换
		String jsonarry = JSONArray.toJSONStringWithDateFormat(userList, "yyyy-MM-dd HH:mm");
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setCount(userList.size());
		result.setData(userList);
		return result;
	}
	// 动态查询所有普通用户
		@RequestMapping("/finduserdynamic")
		@ResponseBody
		public Result findUserDynamic(
				@RequestParam(value = "username") String username,
				@RequestParam(value = "useremali") String useremali,
				@RequestParam(value = "usersex") String usersex) {
			if(username.equals("")) {
				username=null;
			}
			if(useremali.equals("")) {
				useremali=null;
			}
			if(usersex.equals("")) {
				usersex=null;
			}
			Users u = new Users();
			u.setUsername(username);
			u.setUseremali(useremali);
			u.setUsersex(usersex);
			u.setUsertype("1");
			List<Users> userList = usersService.findUserDynamic(u);
			// 时间转换
			String jsonarry = JSONArray.toJSONStringWithDateFormat(userList, "yyyy-MM-dd HH:mm");
			Result result = new Result();
			result.setCode(0);
			result.setMsg("成功");
			result.setCount(userList.size());
			result.setData(userList);
			return result;
		}

	// 删除普通用户
	@RequestMapping("/deleteuser")
	@ResponseBody
	public String deleteUsers(@RequestParam(value = "userid") String username) {
		int ret = usersService.deleteUser(username);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}

	// 修改普通用户
	@RequestMapping("/updateuser")
	@ResponseBody
	public String updateUsers(
			@RequestParam(value = "userloginname") String userloginname,
			@RequestParam(value = "username") String realusername,
			@RequestParam(value = "userloginpassword") String userloginpassword,
			@RequestParam(value = "sex") String sex,
			@RequestParam(value = "usertel") String usertel,
			@RequestParam(value = "useremali") String useremali,
			@RequestParam(value = "usertype") String usertype) {
		Users u = new Users();
		u.setUserloginname(userloginname);
		u.setUsername(realusername);
		u.setUserloginpassword(userloginpassword);
		u.setUsersex(sex);
		u.setUsertel(usertel);
		u.setUseremali(useremali);
		u.setUsertype(usertype);
		int ret = usersService.updateteUser(u);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}
	
	// 管理员添加用户
		@RequestMapping("/adminadduser")
		@ResponseBody
		public String AdminAddUsers(
				@RequestParam(value = "userloginname") String userloginname,
				@RequestParam(value = "username") String realusername,
				@RequestParam(value = "userloginpassword") String userloginpassword,
				@RequestParam(value = "sex") String sex,
				@RequestParam(value = "usertel") String usertel,
				@RequestParam(value = "useremali") String useremali,
				@RequestParam(value = "usertype") String usertype) {
			Users u = new Users();
			u.setUserloginname(userloginname);
			u.setUsername(realusername);
			u.setUserloginpassword(userloginpassword);
			u.setUsersex(sex);
			u.setUsertel(usertel);
			u.setUseremali(useremali);
			u.setUsertype(usertype);
			int ret = usersService.addUserSelective(u);
			if (ret > 0) {
				return "1";
			} else {
				return "0";
			}
		}
}